//个人用户收藏表
//数据操作模块
var sqlHelper = require('../conf/sqlHelper');


//添加收藏商品
function insertCollect(customer_Id,product_Id,callback) {
	var sqlstr='insert into shop_collect(customer_Id,product_Id)values(?,?)';
    var param=[customer_Id,product_Id];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}

//判断当前商品是否已经存在与收藏里
function validateExistGoodsInCollect(customer_Id,product_Id,callback){
	var sqlstr = 'select count(*) as num from shop_collect where customer_Id=? and product_Id=?';
	var param = [customer_Id,product_Id];

	sqlHelper.ExecuteReader(sqlstr, param, function(result) {
		var count = result[0].num;
		if(count > 0) {
			callback(true);
		} else {
			callback(false);
		}
	});
}

//添加收藏方法
function addCollect(customer_Id,product_Id,callback){	
	var message='';
    //验证当前商品是否已经在收藏中
	validateExistGoodsInCollect(customer_Id,product_Id,function(count) {	
	    console.log('aaa');	
		if(!count){
			//如果收藏没有当前购买商品则将商品添加到收藏,并返回信息
			insertCollect(customer_Id,product_Id,function (result) {
				message='商品已成功加入您的收藏';
				callback(true,message);
			});

		}else{
			//如果收藏内包含当前商品，则返回收藏信息			
		    message='商品已在您的收藏中';
			callback(false,message);			
		}
	});
}

//加载最近收藏
function getNewlyCollect(customer_Id,topNum,callback) {
	var sqlstr='select * from shop_collect,shop_product where shop_collect.product_Id=shop_product.product_Id AND shop_collect.customer_Id=? LIMIT 0,?;';
    var param=[customer_Id,topNum];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results){   
    		callback(results);
    	}    		
    });
}

//获取所有收藏分页数据信息
function getCollectListPager(customer_Id,currentPage,pageSize,callback) {
	//计算起始数据
    //起始计数从0开始
    var startIndex=(currentPage-1)*pageSize;
    
    var sqlstr='select * from shop_collect,shop_product where shop_collect.product_Id=shop_product.product_Id AND shop_collect.customer_Id=? LIMIT ?,?';
    var param=[customer_Id,startIndex,pageSize];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results);
            callback(results);
        }
    }); 
}

//获取收藏总行数
function getCollectCounts(customer_Id,callback){
    var sqlstr='select count(*) as count from shop_collect where customer_Id=?';
    var param=[customer_Id];    

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
        if(results){
            //console.log('results:'+results[0].count);
            callback(results[0].count);
        }
    }); 
}

//移除收藏
function removeCollect(customer_Id,product_Id,callback) {
	var sqlstr='delete from shop_collect where customer_Id=? and product_Id=?';
    var param=[customer_Id,product_Id];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}

exports.insertCollect=insertCollect;
exports.addCollect=addCollect;
exports.getNewlyCollect=getNewlyCollect;
exports.getCollectListPager=getCollectListPager;
exports.getCollectCounts=getCollectCounts;
exports.removeCollect=removeCollect;